﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace auxiliary_proj
{
   static class Program
    {
        
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 

        public static string sServer, sUser, sPassword, sDatabase;
        //private static string uUser, uPassword;
        //use Program.con()
        private static string conString;

       //START UP LOCATION
        private static string[] s = Regex.Split(Application.StartupPath, "bin");
       public static string PATH = s[0];


        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //Singleton MainForm
            MainForm mf = Singleton.getMainForm();
            mf.StartPosition = FormStartPosition.CenterScreen;
            Application.Run(mf);     

        }

        public static void parseServerInfo()
        {
            //bring in server.config file and set the values
            try
            {
                string serverInfo = PATH + "/Resources/server.ini";

                //test if see if the file exists, if not create
                if (!File.Exists(PATH + "/Resources/server.ini"))
                {
                    using (StreamWriter output = new StreamWriter(serverInfo))
                    {
                        output.WriteLine("server=localhost");
                        output.WriteLine("database=_its");
                        output.WriteLine("user=root");
                        output.WriteLine("pass=");
                    }
                }

                using (StreamReader sr = new StreamReader(serverInfo))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] temp = line.Split('=');

                        if (temp[0] == "server")
                            sServer = temp[1];
                        else if (temp[0] == "user")
                            sUser = temp[1];
                        else if (temp[0] == "pass")
                            sPassword = temp[1];
                        else if (temp[0] == "database")
                            sDatabase = temp[1];
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine();
        }

        public static void getConnection(string server, string database, string user, string password)
        {
            conString = "server=" + sServer + ";User Id=" + sUser + ";Password=" + sPassword + ";database=" + sDatabase + ";";
        }

        public static string getConnection()
        {
            return "server=" + sServer + ";User Id=" + sUser + ";Password=" + sPassword + ";database=" + sDatabase + ";";
        }

        public static string con()
        {
            return conString;
        }
    }
}
